package main.java.DMDE.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.GroupLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.jgoodies.forms.layout.FormLayout;

import main.java.DMDE.controllers.IInterfaceController;
import main.java.DMDE.controllers.InterfaceController;

public class InterfaceView extends CommonEntityView implements IInterfaceView {

    private IInterfaceController interfaceController;
    private static String TITLE = "Interface Creation";
    
    public InterfaceView() {

	initInterfaceComponents();
	createButton.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent actionEvent) {
		createInterface();
	    }
	});
    }
    
    private void initInterfaceComponents() {
	initComponents();
	frame1.setTitle(TITLE);
    }
    
    private void createInterface(){
	if (interfaceController.addInterface()) {
	    this.frame1.setVisible(false);
	} else {
	    showWrongClassNameDialog();
	}
    }
    
    @Override
    public void setController(InterfaceController interfaceController) {
	this.interfaceController = interfaceController;
    }

    @Override
    public void setInterfaceName(String name) {
	this.className.setText(name);
    }

    @Override
    public String getInterfaceName() {
	return className.getText();
    }

    @Override
    protected void initGeneralPanel() {
 	frame1ContentPane.setLayout(new FormLayout("74*(default, $lcgap), default",
		"73*(default, $lgap), default"));

	generalPanel = new JPanel();
	classNameText = new JLabel();
	className = new JTextField();
	classNameText.setText("Interface name: ");
	classNameText.setLabelFor(className);
	
	GroupLayout generalPanelLayout = new GroupLayout(generalPanel);
	generalPanel.setLayout(generalPanelLayout);

	generalPanelLayout.setAutoCreateGaps(true);
	generalPanelLayout.setAutoCreateContainerGaps(true);

	generalPanelLayout.setHorizontalGroup(generalPanelLayout
		.createSequentialGroup()
		.addGap(30)
		.addGroup(
			generalPanelLayout
				.createParallelGroup(
					GroupLayout.Alignment.LEADING)
				.addComponent(classNameText))
		.addGroup(
			generalPanelLayout
				.createParallelGroup(
					GroupLayout.Alignment.LEADING)
				.addComponent(className)));
	generalPanelLayout.setVerticalGroup(generalPanelLayout
		.createSequentialGroup()
		.addGap(30)
		.addGroup(
			generalPanelLayout
				.createParallelGroup(
					GroupLayout.Alignment.BASELINE)
				.addComponent(classNameText)
				.addComponent(className)));
	
    }

    @Override
    public void addOperationView(IOperationView operatView) {
	addCommonOperationView(operatView); 
	operatView.disableInterfaceVisibility();
	operatView.disableInterfaceModifier();
    }

}
